package com.yh.workbill.ui.companyInfo;


import com.yh.workbill.db.Company;
import com.yh.workbill.model.WorkBill;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

public class CompanyListInfoViewModel extends ViewModel {
    private CompanyInfoRepository repository;

    public CompanyListInfoViewModel() {
        this.repository = new CompanyInfoRepository();
    }

    public LiveData<List<Company>> getAllCompanys() {
        return repository.data;
    }

    public void del(Company company) {
        this.repository.del(company);
    }

    public void add(Company company) {
        this.repository.add(company);
    }


    public MutableLiveData<List<com.yh.workbill.model.Company>> companydatas;

    public MutableLiveData<List<com.yh.workbill.model.Company>> getCompanydatas() {
        if (companydatas == null) {
            companydatas = new MutableLiveData<>();
        }
        return companydatas;
    }

    public void addCompanydatas(int index, com.yh.workbill.model.Company... companys) {
        List<com.yh.workbill.model.Company> value = companydatas.getValue();
        if (value == null) {
            value = new ArrayList<>();
        }
        List<com.yh.workbill.model.Company> temp = (List<com.yh.workbill.model.Company>) Arrays.asList(companys);
        if (index == 0) {//新增
            value.addAll(0, temp);
            companydatas.postValue(value);
        } else {//更多
            value.addAll(temp);
            companydatas.postValue(value);
        }
    }

    public void update() {
        List<com.yh.workbill.model.Company> value = companydatas.getValue();
        if (value == null) {
            value = new ArrayList<>();
        }
        companydatas.postValue(value);
    }
}
